OLDURL = "http://osgbullet.googlecode.com/files/osgBullet_%(VERSION)s.tar.gz"
URL = "http://osgbullet.googlecode.com/svn/trunk"
VERSIONS = ["02_01_00"]
DEFAULT_VERSION = "02_01_00"
DEPENDS = ["osg", "osgworks", "bullet"]

#note after upgrading osg to a new version osgWorks and osgBullet should also be rebuild
#this version syncing is not managed by buildext

#bullet collada support not configured

from source.builders import CMakeBuilder, FetchSourceSVN
import os

class FetchSourceOsgBullet(FetchSourceSVN):

  def fetch(self, url, downloadlocation, buildlocation ):
    targetdir = "osgBullet-%s" % DEFAULT_VERSION #todo allow custom version
    targetpath = buildlocation + os.sep  + targetdir
    if os.path.exists( targetpath ):
      os.system( self.executable + " update " + targetpath  )
    else:
      print self.executable + " checkout " + url + " " + "osgbullet-read-only" + " " + targetpath
      os.system( self.executable + " checkout " + url + " " + "osgbullet-read-only" + " " + targetpath )
    return targetpath + os.sep + "trunk"

class OsgBulletBuilder( CMakeBuilder ):
  """ subclass of CMakeBuilder with custom configure step """

  def downloadextract(self):
    return FetchSourceOsgBullet()

  def targetpath(self):
    return self.directories.enviro + os.sep + "osgBullet_%s_osg-%s" % (self.version, self.dependencies['osg'].target.build_version)
  
  def configure( self ):
    """put osg directories in the environment before cmake configure"""
    
    CMAKE_CONFIGURE = r" -DCMAKE_DEBUG_POSTFIX='d' -DOSGBULLET_USE_DOUBLE_PRECISION=TRUE"
          
    if self.machine.isLinux() and self.machine.bits == 64:
      #osgworks does not find lib64 folder on linux 64bits
      OSG_BUILD_DIR = r" -DOSG_BUILD_DIR="+self.dependencies['osg'].targetpath()+os.sep+"lib64"
      OSG_SOURCE_DIR = r" -DOSG_SOURCE_DIR="+self.dependencies['osg'].targetpath()
      OSG_ROOT = OSG_BUILD_DIR+OSG_SOURCE_DIR

      OSGBuildRoot = r" -DOSGBuildRoot="+self.dependencies['osg'].targetpath()+os.sep+"lib64"
      OSGSourceRoot = r" -DOSGSourceRoot="+self.dependencies['osg'].targetpath()
      OSGInstallLocation = OSGBuildRoot+OSGSourceRoot

      OSGWORKS_BUILD_DIR = r" -DOSGWORKS_BUILD_DIR="+self.dependencies['osgworks'].targetpath()+os.sep+"lib"
      OSGWORKS_SOURCE_DIR = r" -DOSGWORKS_SOURCE_DIR="+self.dependencies['osgworks'].targetpath()
      OSGWORKS_ROOT = OSGWORKS_BUILD_DIR+OSGWORKS_SOURCE_DIR


    else:
      OSG_ROOT = r" -DOSG_ROOT="+self.dependencies['osg'].targetpath()
      OSGWORKS_ROOT = " -DOSGWORKS_ROOT="+self.dependencies['osgworks'].targetpath()
      OSGWORKS_ROOT = " -DOSGWORKS_ROOT="+self.dependencies['osgworks'].targetpath()
      osgWorks_DIR = " -DosgWorks_DIR="+self.dependencies['osgworks'].targetpath()+os.sep+"lib"
      OSGInstallLocation = " -DOSGInstallType=\"Alternate Install Location\" -DOSGInstallLocation="+self.dependencies['osg'].targetpath()

    if self.machine.isLinux():
      print "replacement in CMakeLists: debug libraries in bin folder not found"
      replacement = [( r"    \$\{EXECUTABLE_OUTPUT_PATH\}\/Debug", r"    ${EXECUTABLE_OUTPUT_PATH}" )]
      self.replaceLinesInFile(self.extractedpath + "/CMakeLists.txt", replacement )

    if self.version == "01_00_00":
      #bullet install with include/bullet, so we use the bullet builddir
      bullet_builddir =  self.directories.build+os.sep+"bullet-"+self.dependencies['bullet'].version
      BULLET_SOURCE_DIR = " -DBULLET_SOURCE_DIR="+bullet_builddir+os.sep+"src"
      BULLET_BUILD_DIR = " -DBULLET_BUILD_DIR="+self.dependencies['bullet'].targetpath()
      CMakeBuilder.configure(self, OSG_ROOT+BULLET_SOURCE_DIR+BULLET_BUILD_DIR+OSGWORKS_ROOT+CMAKE_CONFIGURE)
    else:
      BulletInstallLocation = " -DBulletInstallType=\"Alternate Install Location\" -DBulletInstallLocation="+self.dependencies['bullet'].targetpath()
      BULLET_INCLUDE_DIR = " -DBULLET_INCLUDE_DIR="+self.dependencies['bullet'].targetpath()+os.sep+"include"+os.sep+"bullet"
      CMakeBuilder.configure(self, OSGInstallLocation+BulletInstallLocation+OSGWORKS_ROOT+osgWorks_DIR+CMAKE_CONFIGURE+BULLET_INCLUDE_DIR)

BUILDER = OsgBulletBuilder

